from rest_framework import serializers

from .models import *


# 题目标签
class ProblemTagSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProblemTag
        fields = '__all__'


# 题目列表
class ProblemListSerializer(serializers.ModelSerializer):
    level = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()

    class Meta:
        model = Problem
        fields = ['id', 'title_en', 'title_cn', 'level', 'tags', 'passes']

    def get_level(self, obj):
        level_dict = dict(Problem.LEVEL_CHOICES)
        return level_dict.get(obj.level, obj.level)

    def get_tags(self, obj):
        return [tag.tag_cn for tag in obj.tags.all()]


# 题目详情
class ProblemDescriptionSerializer(serializers.ModelSerializer):
    level = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()
    header = serializers.SerializerMethodField()

    class Meta:
        model = Problem
        fields = ['id', 'title_en', 'title_cn', 'level', 'description', 'header', 'cpu_limit', 'mem_limit',
                  'tags']

    def get_level(self, obj):
        level_dict = dict(Problem.LEVEL_CHOICES)
        return level_dict.get(obj.level, obj.level)

    def get_tags(self, obj):
        return [tag.tag_cn for tag in obj.tags.all()]
    
    def get_header(self, obj):
        headers = ProblemHeader.objects.filter(problem=obj)
        if headers.exists():
            header = headers.first()  # 假设每个题目只有一个头部信息
            return {
                'cpp_header': header.cpp_header,
                'java_header': header.java_header,
                'python3_header': header.python3_header,
            }
        return None



# 题目评论列表
class CommentListSerializer(serializers.ModelSerializer):
    user_avatar = serializers.ReadOnlyField(source = 'user.avatar_url')
    username = serializers.ReadOnlyField(source = 'user.username')
    create_date = serializers.DateTimeField(format = '%Y-%m-%d %H:%M', source = 'create_datetime')

    class Meta:
        model = Comment
        fields = ['id', 'user_avatar', 'username', 'content', 'create_date']


# 题目评论
class CommentSerializer(serializers.ModelSerializer):
    user_id = serializers.IntegerField()
    problem_id = serializers.IntegerField()

    class Meta:
        model = Comment
        fields = ['user_id', 'problem_id', 'content']


# 提交记录
class SubmissionListSerializer(serializers.ModelSerializer):
    create_date = serializers.DateTimeField(format = '%Y-%m-%d %H:%M:%S', source = 'submit_datetime')

    class Meta:
        model = Submission
        fields = ['id', 'status', 'create_date']


# 提交记录详情
class SubmissionDetailSerializer(serializers.ModelSerializer):
    problem_id = serializers.IntegerField()
    user_id = serializers.IntegerField()
    create_date = serializers.DateTimeField(format = '%Y-%m-%d %H:%M:%S', source = 'submit_datetime', read_only = True)

    class Meta:
        model = Submission
        fields = ['id','problem_id', 'user_id', 'status', 'status_detail', 'submit_code', 'create_date', 'use_time', 'use_memory', 'language']
